/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseContactDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseContactDao : BaseDao, IBaseDao
    {
        public BaseContactDao()
        {
            base.CurrentTableName = BaseContactTable.TableName;
        }

        public BaseContactDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseContactDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseContactDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseContactEntity baseContactEntity)
        {
            return this.AddEntity(baseContactEntity);
        }

        public string AddEntity(BaseContactEntity baseContactEntity)
        {
            if (string.IsNullOrEmpty(baseContactEntity.ID) || string.IsNullOrEmpty(baseContactEntity.SortCode))
            {
                string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseContactTable.TableName);
                if (string.IsNullOrEmpty(baseContactEntity.ID))
                {
                    baseContactEntity.ID = sequence;
                }
                if (string.IsNullOrEmpty(baseContactEntity.SortCode))
                {
                    baseContactEntity.SortCode = sequence;
                }
            }
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseContactTable.TableName);
            sqlBuilder.SetValue(BaseContactTable.FieldID, baseContactEntity.ID);
            this.SetEntity(sqlBuilder, baseContactEntity);
            sqlBuilder.SetValue(BaseContactTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetValue(BaseContactTable.FieldCreateUserRealName, base.UserInfo.Realname);
            sqlBuilder.SetDBNow(BaseContactTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return baseContactEntity.ID;
        }

        public BaseContactEntity GetEntity(string id)
        {
            return new BaseContactEntity(this.Get(id));
        }

        public int GetNewCount(string userID)
        {
            int num = 0;
            string[] strArray = new string[] { " SELECT COUNT(*)    FROM ", BaseContactDetailsTable.TableName, "  WHERE (", BaseContactDetailsTable.FieldIsNew, " = ", 1.ToString(), " )         AND (", BaseContactDetailsTable.FieldReceiverID, " = '", userID, "' )" };
            string sqlQuery = string.Concat(strArray);
            object obj2 = base.DbHelper.ExecuteScalar(sqlQuery);
            if (obj2 != null)
            {
                num = int.Parse(obj2.ToString());
            }
            return num;
        }

        public DataTable SearchPublic(string search, bool deleteMark)
        {
            DataTable dataTable = new DataTable(BaseContactTable.TableName);
            string sqlQuery = string.Concat(new object[] { " SELECT ECP_Contact.Title  ,ECP_Contact.SendCount ,ECP_Contact.ReplyCount ,ECP_Contact.ReadCount ,ECP_Contact.CreateDate ,ECP_Contact.", BaseContactTable.FieldSortCode, " FROM ECP_Contact WHERE (ECP_Contact.", BaseContactTable.FieldIsPublic, " = 1 )        AND (ECP_Contact.", BaseContactTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ")" });
            if (!string.IsNullOrEmpty(search))
            {
                string str2 = sqlQuery;
                string str3 = str2 + " AND (" + BaseContactTable.FieldTitle + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldTitle);
                sqlQuery = str3 + " OR " + BaseContactTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldContent) + ")";
                search = BaseBusinessLogic.GetSearchString(search);
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldTitle, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldContent, search));
            }
            sqlQuery = sqlQuery + " ORDER BY ECP_Contact." + BaseContactTable.FieldSortCode + " DESC ";
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable SearchReceive(string userID, string search, bool deleteMark)
        {
            return this.SearchReceive(userID, search, deleteMark, 0);
        }

        public DataTable SearchReceive(string userID, string search, bool deleteMark, int topN)
        {
            DataTable dataTable = new DataTable(BaseContactTable.TableName);
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT ";
            if (topN != 0)
            {
                sqlQuery = sqlQuery + " TOP " + topN.ToString();
            }
            object obj2 = sqlQuery;
            sqlQuery = string.Concat(new object[] {
                obj2, " ECP_ContactDetails.ID  , ECP_ContactDetails.ContactID  , ECP_ContactDetails.IsNew  , ECP_ContactDetails.NewComment  , ECP_ContactDetails.Enabled  , ECP_Contact.Title  , ECP_Contact.Priority  , ECP_Contact.SendCount  , ECP_Contact.ReadCount  , ECP_Contact.CommentUserID  , ECP_Contact.CommentUserRealName  , ECP_Contact.CommentDate  , ECP_Contact.CreateDate  , ECP_Contact.CreateUserID  , ECP_Contact.CreateUserRealName  FROM ECP_ContactDetails LEFT OUTER JOIN ECP_Contact ON ECP_ContactDetails.ContactID = ECP_Contact.ID  WHERE ( ECP_Contact.", BaseContactTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ") AND (ECP_ContactDetails.", BaseContactDetailsTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ") AND ((ECP_ContactDetails.", BaseContactDetailsTable.FieldReceiverID, "= '", userID, "' AND (ECP_Contact.CreateUserID != '", userID, "'OR ECP_ContactDetails.Enabled = 1))  OR (ECP_ContactDetails.",
                BaseContactDetailsTable.FieldReceiverID, "= '", userID, "' AND ECP_ContactDetails.NewComment = 1))"
             });
            if (!string.IsNullOrEmpty(search))
            {
                string str2 = sqlQuery;
                string str3 = str2 + " AND (" + BaseContactTable.FieldTitle + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldTitle);
                sqlQuery = str3 + " OR " + BaseContactTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldContent) + ")";
                search = BaseBusinessLogic.GetSearchString(search);
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldTitle, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldContent, search));
            }
            sqlQuery = ((sqlQuery + " ORDER BY ECP_ContactDetails." + BaseContactDetailsTable.FieldIsNew + " DESC ") + " ,ECP_ContactDetails." + BaseContactDetailsTable.FieldNewComment + " DESC ") + " ,ECP_Contact." + BaseContactTable.FieldSortCode + " DESC ";
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable SearchSent(string userID, string search, bool deleteMark)
        {
            DataTable dataTable = new DataTable(BaseContactTable.TableName);
            string sqlQuery = string.Concat(new object[] { " SELECT ECP_Contact.Title  ,ECP_Contact.SendCount ,ECP_Contact.ReplyCount ,ECP_Contact.ReadCount ,ECP_Contact.CreateDate ,ECP_Contact.", BaseContactTable.FieldSortCode, " ,ECP_ContactDetails.ID ,ECP_ContactDetails.NewComment FROM ECP_Contact INNER JOIN  ECP_ContactDetails ON ECP_Contact.ID = ECP_ContactDetails.ContactID  WHERE (ECP_Contact.CreateUserID = '", userID, "')  AND (ECP_ContactDetails.ReceiverID = '", userID, "') AND (ECP_ContactDetails.", BaseContactDetailsTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ") AND (ECP_Contact.", BaseContactTable.FieldDeleteMark, " = ", deleteMark ? 1 : 0, ")" });
            if (!string.IsNullOrEmpty(search))
            {
                string str2 = sqlQuery;
                string str3 = str2 + " AND (" + BaseContactTable.FieldTitle + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldTitle);
                sqlQuery = str3 + " OR " + BaseContactTable.FieldContent + " LIKE " + base.DbHelper.GetParameter(BaseContactTable.FieldContent) + ")";
                search = BaseBusinessLogic.GetSearchString(search);
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldTitle, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseContactTable.FieldContent, search));
            }
            sqlQuery = (sqlQuery + " ORDER BY ECP_ContactDetails." + BaseContactDetailsTable.FieldNewComment + " DESC ") + " ,ECP_Contact." + BaseContactTable.FieldSortCode + " DESC ";
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public int Send(string contactID, string[] receiverIDs)
        {
            int num = 0;
            bool flag = false;
            BaseContactDetailsDao dao = new BaseContactDetailsDao(base.DbHelper, base.UserInfo);
            BaseUserDao dao2 = new BaseUserDao(base.DbHelper, base.UserInfo);
            BaseUserEntity entity = null;
            BaseContactDetailsEntity baseContactDetailsEntity = null;
            for (int i = 0; i < receiverIDs.Length; i++)
            {
                entity = dao2.GetEntity(receiverIDs[i]);
                if (entity.Enabled && !entity.DeleteMark)
                {
                    baseContactDetailsEntity = new BaseContactDetailsEntity
                    {
                        ID = BaseSequenceDao.Instance.NewGuid(),
                        ContactID = contactID,
                        ReceiverID = entity.ID,
                        ReceiverRealName = entity.Realname,
                        IsNew = true,
                        Enabled = true,
                        NewComment = false
                    };
                    dao.Add(baseContactDetailsEntity);
                }
                if (!flag && base.UserInfo.ID.Equals(receiverIDs[i]))
                {
                    flag = true;
                }
                num++;
            }
            if (!flag)
            {
                entity = dao2.GetEntity(base.UserInfo.ID);
                baseContactDetailsEntity = new BaseContactDetailsEntity
                {
                    ID = BaseSequenceDao.Instance.NewGuid(),
                    ContactID = contactID,
                    ReceiverID = entity.ID,
                    ReceiverRealName = entity.Realname,
                    IsNew = false,
                    Enabled = false,
                    NewComment = false
                };
                dao.Add(baseContactDetailsEntity);
                num++;
            }
            this.SetProperty(BaseContactTable.FieldSendCount, receiverIDs.Length.ToString());
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseContactEntity baseContactEntity)
        {
            sqlBuilder.SetValue(BaseContactTable.FieldReceiverRealName, baseContactEntity.ReceiverRealName);
            sqlBuilder.SetValue(BaseContactTable.FieldTitle, baseContactEntity.Title);
            sqlBuilder.SetValue(BaseContactTable.FieldContent, baseContactEntity.Content);
            sqlBuilder.SetValue(BaseContactTable.FieldPriority, baseContactEntity.Priority);
            sqlBuilder.SetValue(BaseContactTable.FieldSendCount, baseContactEntity.SendCount);
            sqlBuilder.SetValue(BaseContactTable.FieldReadCount, baseContactEntity.ReadCount);
            sqlBuilder.SetValue(BaseContactTable.FieldReplyCount, baseContactEntity.ReplyCount);
            sqlBuilder.SetValue(BaseContactTable.FieldIsPublic, baseContactEntity.IsPublic);
            sqlBuilder.SetValue(BaseContactTable.FieldCommentUserID, baseContactEntity.CommentUserID);
            sqlBuilder.SetValue(BaseContactTable.FieldCommentUserRealName, baseContactEntity.CommentUserRealName);
            sqlBuilder.SetValue(BaseContactTable.FieldCommentDate, baseContactEntity.CommentDate);
            sqlBuilder.SetValue(BaseContactTable.FieldDeleteMark, baseContactEntity.DeleteMark);
            sqlBuilder.SetValue(BaseContactTable.FieldEnabled, baseContactEntity.Enabled);
            sqlBuilder.SetValue(BaseContactTable.FieldDescription, baseContactEntity.Description);
            sqlBuilder.SetValue(BaseContactTable.FieldSortCode, baseContactEntity.SortCode);
        }

        public int Update(BaseContactEntity baseContactEntity)
        {
            return this.UpdateEntity(baseContactEntity);
        }

        public int UpdateEntity(BaseContactEntity baseContactEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseContactTable.TableName);
            this.SetEntity(sqlBuilder, baseContactEntity);
            sqlBuilder.SetValue(BaseContactTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseContactTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseContactTable.FieldID, baseContactEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}